package com.ashye.bindingrecyclerviewdemo;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangwei on 2017/6/28.
 */
public class FooterBindingRecyclerAdapter<T> extends RecyclerView.Adapter<FooterBindingRecyclerAdapter.BindingViewHolder>
        implements View.OnClickListener {

    enum ViewType {
        TYPE_ITEM,
        TYPE_FOOTER
    }


    private final List<T> listData = new ArrayList<>();
    private int layoutId;
    private int brId;

    private boolean showFooter = true;
    private int footerStatus = 0;


    public FooterBindingRecyclerAdapter(int lid, int bid) {
        layoutId = lid;
        brId = bid;
    }

    public void addData(List<T> data) {
        int size = listData.size();
        listData.addAll(data);
        notifyItemRangeInserted(size, data.size());
    }

    public void addData(T item) {
        listData.add(item);
        notifyItemInserted(listData.size() - 1);
    }

    public void setData(List<T> list) {
        listData.clear();
        addData(list);
    }

    public T getItemData(int position) {
        return listData.get(position);
    }

    @Override
    public void onClick(View view) {
        if (view.getTag() instanceof Integer) {
            int tag = (int) view.getTag();
            onItemClicked(view, tag, listData.get(tag));
        }
    }

    public void onItemClicked(View view, int position, T item) {

    }

    @Override
    public int getItemViewType(int position) {
        if (getFooterCount() > 0 && position == getItemCount() - 1) {
            return ViewType.TYPE_FOOTER.ordinal();
        } else {
            return ViewType.TYPE_ITEM.ordinal();
        }
    }

    @Override
    public BindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ViewType.TYPE_FOOTER.ordinal()) {
            TextView textView = new TextView(parent.getContext());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.height = 150;
            textView.setLayoutParams(params);
            textView.setGravity(Gravity.CENTER);
            textView.setTextColor(textView.getContext().getResources().getColor(android.R.color.secondary_text_dark));
            return new BindingViewHolder(
                    textView, false
            );
        } else {
            BindingViewHolder holder = new BindingViewHolder(
                    LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false),
                    true
            );

            holder.itemView.setOnClickListener(this);
            return holder;
        }
    }

    @Override
    public void onBindViewHolder(BindingViewHolder holder, int position) {
        if (getItemViewType(position) == ViewType.TYPE_FOOTER.ordinal()) {
            if (!showFooter) {
                return;
            }
            ((TextView)holder.itemView).setText(getFooterString());
        } else if (getItemViewType(position) == ViewType.TYPE_ITEM.ordinal()) {
            holder.binding.setVariable(brId, listData.get(position));
            holder.binding.executePendingBindings();
            holder.itemView.setTag(position);
        }
    }


    public void hideFooter() {
        if (showFooter) {
            showFooter = false;
            notifyItemRemoved(getItemCount());
        }
    }

    public void loading(boolean loading) {

        footerStatus = loading ? 1 : 0;
        if (showFooter) {
            notifyItemChanged(getItemCount() - 1);
        } else {
            showFooter = true;
            notifyItemInserted(getItemCount());
        }
    }

    public void loadAll() {
        footerStatus = 2;
        if (showFooter) {
            notifyItemChanged(getItemCount() - 1);
        } else {
            showFooter = true;
            notifyItemInserted(getItemCount());
        }
    }

    private String getFooterString() {
        switch (footerStatus) {
            case 0:
                return "加载更多";
            case 1:
                return "正在加载";
            case 2:
                return "全部加载";
            default:
                return "";
        }
    }

    @Override
    public long getItemId(int position) {
        return showFooter ? position - 1 : position;
    }

    @Override
    public int getItemCount() {
        return listData.size() + getFooterCount();
    }

    private int getFooterCount() {
        return showFooter ? 1 : 0;
    }


    public static class BindingViewHolder extends RecyclerView.ViewHolder {
        public final ViewDataBinding binding;

        public BindingViewHolder(View view, boolean binding) {
            super(view);
            if (binding) {
                this.binding = DataBindingUtil.bind(view);
            }else {
                this.binding = null;
            }
        }
    }

}
